package com.itheima.web;

import com.itheima.Service.UserService;
import com.itheima.dao.pojo.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/pageServlet")
public class pageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得请求中的数据
        Integer currentPage =Integer.parseInt(request.getParameter("currentPage")) ;
        //创建对象调用方法
        UserService userService = new UserService();
        //获取每页的人数
        ArrayList<User> users = userService.pagingUser(currentPage);
        //获取最大的个数
        Integer maxItem = userService.getMax();
        //获取需要分的页数
        Integer maxPage = (maxItem/5)+1;
        //将数据都传递到域中
        request.setAttribute("currentPage",currentPage);
        request.setAttribute("users",users);
        request.setAttribute("maxPage",maxPage);
        //跳转页面
        request.getRequestDispatcher("/pageList.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
